<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/name.php';

$db = new DB('star');

$category_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 0;
$search_title_id = isset($_GET['search_title_id']) ? h($_GET['search_title_id']) : '';
$search_title = isset($_GET['search_title']) ? h($_GET['search_title']) : '';
$search_start_time = isset($_GET['search_start_time']) ? strtotime(h($_GET['search_start_time'])) : 0;
$search_end_time = isset($_GET['search_end_time']) ? strtotime(h($_GET['search_end_time'])) : 0;
$search_rs = array();
$search_start_time_string = '';
$search_end_time_string = '';

if( isset($_GET['search']) && $_GET['search']=='搜索'){
	$now_time = date('Y-m-d',time());
	
	$search_sql = 'select * from star where ';
	if($search_title_id != ''){
		$search_sql .= 'title_id like "%'.$search_title_id.'%" and ';
	}
	if($search_title != ''){
		$search_sql .= 'title like "%'.$search_title.'%" and ';
	}
	
	if( $search_start_time > 0 || $search_end_time > 0 ){//选择了时间
		if($search_start_time > 0){
			$search_start_time_string = date('Y-m-d',$search_start_time);
		}else{
			$search_start_time_string = $now_time;
		}
		if($search_end_time > 0){
			$search_end_time_string = date('Y-m-d',$search_end_time);
			$search_end_time = $search_end_time + 60*60*24;
		}else{
			$search_end_time_string = $now_time;
		}

		if($search_start_time < $search_end_time){//形成了区间
			$search_sql .= 'created > '.$search_start_time.' and created < '.$search_end_time.' and ';
		}else{
			$search_start_time_string = '';
			$search_end_time_string = '';
		}
	}
	
	$search_sql .= 'is_del=1 order by index_top desc,orders desc,updated desc';
	$search_rs = $db->query($search_sql)->getAll();
	
	$news_db = new DB('news');
	foreach( $search_rs as $key=>$val ){
		//查询姓名
		$create_username = Name::get_username($news_db, $val['create_uid']);
		$search_rs[$key]['create_username'] = $create_username;
	}
}

//包含模板
require template('admin/star/star_search');